
define(function(require, exports, module) {

    "use strict";
    var method = require("method");
    var Pikaday = require("pikaday");


    Vue.directive('allcheck', {
        twoWay: true,
        update: function (obj) {
            var me = this;
            var $el = $(this.el).find("[check]");
            var $tip = $("<span class='m_line_tip error' style='display:none'></span>");
            $el.after($tip);
            // input blur
            $el.on("input", function(){
                checkInput($(this));
            });

            obj.$valid = function(){
                var flagObj = null;
                $el.each(function(i, sel){
                    var r = checkInput($(sel));
                    if (flagObj) return;
                    if (!r.flag) {
                        flagObj = r;
                    }
                });
                obj.flag = flagObj ? flagObj.flag : true;
                obj.tip = flagObj ? flagObj.tip : "";
                me.set(obj);
            };
            var checkInput = function($input){
                var $error = $input.next(),
                    r = check($input.val(), $input.attr("check"));
                $error.html(r.tip);
                if (!r.flag) {
                    $error.show();
                } else {
                    $error.hide();
                }
                return r;
            }
            var check = function(val, str){
                str = str.replace(/'/g, "\"");
                var gui = JSON.parse(str);
                var r = {flag: true, tip: ""},
                    j = 0, lb,
                    v, pv, type;
                lb = gui.length;
                for (; j < lb; j++) {
                    if (!r.flag) break;
                    v = gui[j];
                    type = v.type;
                    pv = patternObj[type];
                    if( pv ){
                        // 正则匹配
                        if (pv.regexp) {
                            if (!pv.regexp.test(val)) {
                                r.flag = false;
                                r.tip = !v.tip ? pv.tip : v.tip;
                            }
                        } else if (type === "max") {
                            if (val > v.val) {
                                r.flag = false;
                                r.tip = !v.tip ? (pv.tip + v.val) : v.tip;
                            }
                        } else if (type === "min") {
                            if (val < v.val) {
                                r.flag = false;
                                r.tip = !v.tip ? (pv.tip + v.val) : v.tip;
                            }
                        } else if (type === "length") {
                            if (v.val && val.length !== v.val) {
                                r.flag = false;
                                r.tip = !v.tip ? (pv.tip + "等于" + v.val + "位") : v.tip;
                            } else if (v.min && val.length < v.min) {
                                r.flag = false;
                                r.tip = !v.tip ? (pv.tip + "大于等于" + v.min + "位") : v.tip;
                            } else if (v.max && val.length > v.max) {
                                r.flag = false;
                                r.tip = !v.tip ? (pv.tip + "小于等于" + v.max + "位") : v.tip;
                            }
                        }
                    }
                }
                return r;
            }
            var patternObj = {
                "require": {
                    regexp: /.+/,
                    tip: "不能为空",
                },
                "int":{
                    regexp:/^[0-9]*$/,
                    tip: "须为整数"
                },
                "float":{
                    regexp: /^\d*(\d?\.\d{1,2})?$/,
                    tip: "须为数字,且只能保留两位小数"
                },
                "length" : {
                    tip : "长度值必须为", // 后面补上
                },
                "max" : {
                    tip : "最大值不能超过", // 后面补上
                },
                "min" : {
                    tip : "最小值不能小过", // 后面补上
                },
                "complex":{
                    regexp: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/,
                    tip: "请填写8-16位数字与字母的组合密码"
                },
                "phone":{
                    regexp: /^1[0-9]{10}$/,
                    tip: "请输入正确的手机号"
                },
                "email": {
                    regexp: /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/,
                    tip: "请输入正确的邮箱"
                }
            }
        }
    });

    var i18n = { // 本地化
        previousMonth   : '上个月',
        nextMonth       : '下个月',
        months          : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
        weekdays        : ['周日','周一','周二','周三','周四','周五','周六'],
        weekdaysShort   : ['日','一','二','三','四','五','六']
    }

    Vue.directive('time', {
        twoWay: true,
        bind : function(){
            var me = this;
            new Pikaday({
                field: me.el,
                i18n: i18n,
                // minDate: new Date('2000-01-01'),
                // maxDate: new Date('2020-12-31'),
                // yearRange:   [2000,2020],
                onSelect: function() {
                    var v = method.changeTime(this._d, "yyyymmdd");
                    me.el.innerHTML = v;
                    me.set(v);
                }
            });
        },
        update : function(c){
            var me = this;
            me.el.innerHTML = c;
        }
    });
    

    
    module.exports = Vue;

});
